﻿using System;
using System.Collections.Generic;
using System.Text;
using ArrayUtilHelpV2;
using ByteUtilHelpV2;
using HexUtilHelpV2;

namespace ByteUtilHelpV2Plus
{
    public class ByteToolV2Plus
    {
        public static string ByteArrayToIntExHLStr(byte[] bytes, int startIndex, int endIndex)
        {
            try
            {
                byte[] _bytes = ArrayToolV2.Copy<byte>(bytes, startIndex, endIndex);
                if (_bytes.Length == 2)
                {
                    Array.Reverse(_bytes, 0, 2);//高地位互换
                    string _hex = ByteToolV2.ToHexString(_bytes, ByteArrayToHexType.BitConverter);//转换为十六进制
                    return HexToolV2.ConvertToInt(_hex).ToString();//将十六进制转换成十进制
                }
                return "--";
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("ByteArrayToIntExHL Exception:{0}", ex.Message.Trim()));
            }
        }
        public static int ByteArrayToIntExHL(byte[] bytes, int startIndex, int endIndex)
        {
            try
            {
                byte[] _bytes = ArrayToolV2.Copy<byte>(bytes, startIndex, endIndex);
                if (_bytes.Length == 2)
                {
                    Array.Reverse(_bytes, 0, 2);//高地位互换
                    string _hex = ByteToolV2.ToHexString(_bytes, ByteArrayToHexType.BitConverter);//转换为十六进制
                    return HexToolV2.ConvertToInt(_hex);//将十六进制转换成十进制
                }
                return 0;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("ByteArrayToIntExHL Exception:{0}", ex.Message.Trim()));
            }
        }
        public static string ByteArrayToHex(byte[] bytes, int startIndex, int endIndex)
        {
            try
            {
                byte[] _bytes = ArrayToolV2.Copy<byte>(bytes, startIndex, endIndex);
                return ByteToolV2.ToHexString(_bytes, ByteArrayToHexType.BitConverter);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("ByteArrayToHex Exception:{0}", ex.Message.Trim()));
            }
        }
        public static string ByteArrayToHexWithBlank(byte[] bytes, int startIndex, int endIndex)
        {
            try
            {
                byte[] _bytes = ArrayToolV2.Copy<byte>(bytes, startIndex, endIndex);
                return ByteToolV2.ToHexStringWithBlank(_bytes);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("ByteArrayToHexWithBlank Exception:{0}", ex.Message.Trim()));
            }
        }
        public static string ByteArrayToIntStr(byte[] bytes, int startIndex, int endIndex)
        {
            try
            {
                byte[] _bytes = ArrayToolV2.Copy<byte>(bytes, startIndex, endIndex);
                string _hex = ByteToolV2.ToHexString(_bytes, ByteArrayToHexType.BitConverter);
                return HexToolV2.ConvertToInt(_hex).ToString();
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("ByteArrayToIntStr Exception:{0}", ex.Message.Trim()));
            }
        }
        public static int ByteArrayToInt(byte[] bytes, int startIndex, int endIndex)
        {
            try
            {
                byte[] _bytes = ArrayToolV2.Copy<byte>(bytes, startIndex, endIndex);
                string _hex = ByteToolV2.ToHexString(_bytes, ByteArrayToHexType.BitConverter);
                return HexToolV2.ConvertToInt(_hex);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("ByteArrayToInt Exception:{0}", ex.Message.Trim()));
            }
        }
        public static byte ToByte(int value)
        {
            string _hexStr = HexToolV2.ToHexString(value);
            return Convert.ToByte(_hexStr, 16);
        }
        /// <summary>
        /// 若source大于compare，则取source反。
        /// </summary>
        /// <param name="source"></param>
        /// <param name="compare"></param>
        /// <returns></returns>
        public static int OverPointValueUseReversed(byte[] source, byte[] compare)
        {
            try
            {
                int _resultValue = 0;
                if (source == null)
                    throw new ArgumentNullException("source");
                if (compare == null)
                    throw new ArgumentNullException("compare");

                string _sourceHex = ByteToolV2.ToHexString(source, ByteArrayToHexType.BitConverter);
                int _sourceIntValue = HexToolV2.ConvertToInt(_sourceHex);

                string _compareHex = ByteToolV2.ToHexString(compare, ByteArrayToHexType.BitConverter);
                int _compareIntValue = HexToolV2.ConvertToInt(_compareHex);

                _resultValue = _sourceIntValue > _compareIntValue == true ? ~_sourceIntValue : _sourceIntValue;
                return _resultValue;
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("OverPointValueUseReversed Exception:{0}", ex.Message.Trim()));
            }
        }
        /// <summary>
        /// 若source大于compare，则取source反。
        /// </summary>
        /// <param name="source"></param>
        /// <param name="compare"></param>
        /// <param name="divisorValue">取反后需要除的除数</param>
        /// <returns></returns>
        public static decimal OverPointValueUseReversed(byte[] source, byte[] compare, decimal divisorValue)
        {
            try
            {
                if (divisorValue <= 0)
                    throw new ArgumentException("divisorValue");

                int _resultValue = OverPointValueUseReversed(source, compare);
                return Math.Abs(_resultValue / divisorValue);
            }
            catch (ArgumentException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("OverPointValueUseReversed Exception:{0}", ex.Message.Trim()));
            }
        }
        /// <summary>
        /// 若source大于compare，则取source反。
        /// </summary>
        /// <param name="source"></param>
        /// <param name="compare"></param>
        /// <param name="divisorValue">取反后需要除的除数</param>
        /// <param name="precisionValue">除后，需要保留的精度</param>
        /// <returns></returns>
        public static decimal OverPointValueUseReversed(byte[] source, byte[] compare, decimal divisorValue, int precisionValue)
        {
            try
            {

                if (precisionValue < 0)
                    throw new ArgumentException("precisionValue");
                decimal _resultValue = OverPointValueUseReversed(source, compare, divisorValue);
                return Math.Round(_resultValue, precisionValue);
            }
            catch (ArgumentException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("OverPointValueUseReversed Exception:{0}", ex.Message.Trim()));
            }
        }
    }
}
